//
//  AppendLogsViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/5.
//

#import "AppendLogsViewController.h"
#import "AppendDetailsModel.h"
#import "WarnModel.h"
#import "AppendLogsableViewCell.h"
#import "foundQuestModel.h"
#import "AppendLogsModel.h"
#import "TimeFrameShowView.h"
#import "foundQuestViewController.h"
@interface AppendLogsViewController ()
@property (nonatomic ,strong) NSString * startTime;
@property (nonatomic ,strong) NSString * endTime;
@property (nonatomic ,strong) NSMutableArray<AppendLogsModel *> * modelArray;
@property (nonatomic ,strong) UIButton * upBtn;

@end

@implementation AppendLogsViewController

-(NSMutableArray<AppendLogsModel *> *)modelArray{
    if(!_modelArray){
        _modelArray = [NSMutableArray array];
    }
    return _modelArray;
}
- (NSString *)startTime{
    if(!_startTime){
        _startTime = [NSDate br_stringFromDate:[[NSDate new] br_getNewDateToDays:-6] dateFormat:@"yyyy-MM-dd"];
    }
    return _startTime;
}
- (NSString *)endTime{
    if(!_endTime){
        _endTime = [NSDate br_stringFromDate:[NSDate new] dateFormat:@"yyyy-MM-dd"];
    }
    return _endTime;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"池塘日志" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    if(self.pondname.length > 0){
        leftlab.text = self.pondname;
    }
    [backview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.offset(0);
    }];
    UIButton * zdspBtn = [UIButton buttonWithType:0];
    zdspBtn.addNormalText(@"新增任务").addNormalColor(UIColor.whiteColor).addFont(kCFont(18)).addTarget(self,@selector(addRW));
    [topview addSubview:zdspBtn];
    [zdspBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.centerY.offset(KTop/2);
    }];
    
    
    [self.view addSubview:self.tableview];
    self.tableview.backgroundColor = KcolorHex(@"#E6E9EE");
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.top.equalTo(topview.mas_bottom);
    }];
    
    [self addFooterView:^{
        [weakSelf addMoreData];
    }];
    [self addHeaderView:^{
        [weakSelf refreshingData];
    }];
    
    self.upBtn = [UIButton buttonWithType:0];
    [self.upBtn setBackgroundImage:[UIImage imageNamed:@"top"] forState:UIControlStateNormal];
    [self.upBtn addTarget:self action:@selector(upbtnclick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.upBtn];
    [self.upBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.offset(kwt(-50)- KSafeAreaBottomHeight);
        make.right.offset(kwt(-20));
        make.width.height.offset(kwt(50));
    }];
    [self loadNewData];
}

-(void)refreshingData{
    self.pageNo = 1;
    [self.modelArray removeAllObjects];
    [self loadNewData];
}
- (void)upbtnclick{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    [self.tableview scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)addRW{
    
    foundQuestViewController * vc = [foundQuestViewController new];
    vc.pondid = self.pondId;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)loadNewData{
    
    [self.view showBusyHUD];
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
//    [dict setObject:@(self.pageNo) forKey:@"pageNo"];
//    [dict setObject:@(self.pageSize) forKey:@"pageSize"];
//    [dict setObject:@(5) forKey:@"pageSize"];
    [dict setObject:self.pondId forKey:@"pondId"];
    [dict setObject:self.startTime forKey:@"startTime"];
//    [dict setObject:[NSString stringWithFormat:@"%@ 23:59:59",self.endTime] forKey:@"endTime"];
    [dict setObject:self.endTime forKey:@"endTime"];
    if(self.type > 0){
            [dict setObject:@(self.type) forKey:@"type"];
    }
    [dict setObject:self.endTime forKey:@"endTime"];
    if(self.modelArray.count > 0){
        [dict setObject:self.modelArray.lastObject.cardTime forKey:@"pageTime"];
    }
    [SCNetManger PostCardGetPondTaskLogWithDict:dict andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * array  = [NSArray yy_modelArrayWithClass:AppendLogsModel.class json:model.result];
            [self.modelArray addObjectsFromArray:array];
            if(array.count == 0 && self.modelArray.count > 0){
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableview.mj_footer endRefreshing];
            }
            [self.tableview.mj_header endRefreshing];
        } else {
            [self.view showWarning:model.message];
        }
        [self.tableview reloadData];
    }];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headerview = [UIView new];
    UIView * btnview = [UIView new];
    MJWeakSelf
    btnview.addClickBlock(^{
        [weakSelf openTime];
    });
    [headerview addSubview:btnview];
    btnview.backgroundColor= UIColor.whiteColor;
    [DTKTool YJ:kwt(4) andView:btnview];
    [btnview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(31));
    }];
    UILabel * time = [DTKTool labelWithText:@"-" andFont:kFont(16) andTextColor:KcolorHex(@"#222222")];
    [btnview addSubview:time];
    [time mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.left.offset(kwt(11));
    }];
    
    UIImageView * img = kimageView(@"tangkou_expand");
    [btnview addSubview:img];
    [img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.offset(0);
        make.right.offset(kwt(-11));
        make.width.height.offset(kwt(15));
//        make.height.offset(kwt(7));
//        make.left.equalTo(time.mas_right).offset(kwt(11));
    }];
      
    time.text = [NSString stringWithFormat:@"%@~%@",self.startTime,self.endTime];
   
    return headerview;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return kwt(60);
}

//- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
//    UIView * bkview = [UIView new];
//    bkview.clipsToBounds = YES;
//    UILabel * temp5 = [DTKTool labelWithText:@"更有更多了～" andFont:kFont(14) andTextColor:KcolorHex(@"#787A79")];
//    [bkview addSubview:temp5];
//    [temp5 mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.center.offset(0);
//     
//    }];
//  
//    return bkview;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
//    if(self.modelArray.count > 0){
//        return kwt(50);
//    }
//    return 0;
//}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.modelArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.modelArray[indexPath.row].CellH;
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    AppendLogsableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"AppendLogsableViewCell"];
    if (cell == nil) {
        cell = [[AppendLogsableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"AppendLogsableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.modelArray[indexPath.row];
    MJWeakSelf
    cell.openReloadBlock = ^{
        [weakSelf.tableview reloadData];
    };
    return cell ;
    
}

- (void)openTime{
    MJWeakSelf
    [TimeFrameShowView CZshowviewWithTitle:@"选择时间" andStartTime:self.startTime andEndTime:self.endTime andRightBlock:^(NSString * _Nonnull StartTime, NSString * _Nonnull EndTime) {
            weakSelf.endTime = EndTime;
            weakSelf.startTime = StartTime;
            [weakSelf refreshingData];
    } andCZBlock:^{
        weakSelf.endTime = nil;
        weakSelf.startTime = nil;
        [weakSelf refreshingData];
    }];
}
@end
